<template>
  <div class="loginContainer">
    <div class="topContainer">
      <!-- 跳到首页 -->
      <a href="/" class="home">
        <van-icon name="wap-home-o" size="34" color="#333" />
      </a>
      <a href="/" class="logo">网易严选</a>
      <!-- 跳转到搜索页 -->
      <a href="/search" class="search">
        <van-icon name="search" size="34" color="#333" />
      </a>
      <!-- 跳转到购物车 -->
      <a href="/shopCar" class="shopCar">
        <van-icon name="shopping-cart-o" size="34" color="#333" />
      </a>
    </div>
    <div class="centerContainer">
      <div class="wytext">
        <img src="../../../assets/logo.png" alt="" />
      </div>
      <div class="registerInput">
        <van-form @submit="onSubmit">
          <van-cell-group inset>
            <van-field
              v-model="phone"
              required
              label="手机号"
              placeholder="请输入手机号"
              name="pattern"
              :rules="[{ pattern, message: '手机格式不正确' }]"
            />
            <van-field
              v-model="password"
              required
              label="密码"
              placeholder="请输入注册密码"
              type="password"
              :rules="[{ required: true, message: '请填写密码' }]"
            />
          </van-cell-group>
          <van-cell-group inset>
            <van-field
              v-model="code"
              center
              clearable
              label="短信验证码"
              placeholder="请输入短信验证码"
            >
              <template #button>
                <van-button size="small" type="primary" @click="sendCode"
                  >发送验证码</van-button
                >
              </template>
            </van-field>
          </van-cell-group>
        </van-form>
      </div>
      <div class="userTest">
        <input type="checkbox" />
        <span
          >同意协议并注册<a href="#javascript">《网易严选用户协议》</a></span
        >
      </div>
      <div class="register">
        <van-button type="danger" size="large" @click="userRegister"
          >注册</van-button
        >
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
// 引入注册请求
import { reqRegister, reqCode } from "@/api/lgoin";
import { useRouter } from "vue-router";
const router = useRouter();
// 手机号校验
const phone = ref("");
const pattern = /^1[3-9][0-9]{9}$/;
// 密码校验
const password = ref("");
const code = ref("");
// 收集用户注册信息
const formData = ref({
  // 收集用户手机号
  phone: "",
  // 收集用户密码
  password: "",
  // 收集验证码
  code: "",
});
// 发送验证码
const sendCode = async () => {
  if (!phone.value) {
    alert("请输入手机号");
    return;
  }
  try {
    // 发送请求
    let res = await reqCode(phone.value);
    console.log(res);
    code.value = res.data;
  } catch (error) {
    return Promise.reject(error);
  }
};
// 注册
const userRegister = async () => {
  if (code.value && phone.value && password.value) {
    try {
      await reqRegister({
        phone: phone.value,
        password: password.value,
        code: code.value,
      });
      alert("注册成功");
      // 跳转登录页
      router.push({name: "Email"});
    } catch (error) {
      return Promise.reject(error);
    }
  } else {
    alert("请输入完整注册信息");
  }
};
</script>

<style scoped lang="less">
* {
  margin: 0;
  padding: 0;
}
.loginContainer {
  height: 100%;
}
.topContainer {
  width: 100%;
  height: 47px;
  background-color: #fafafa;
  line-height: 47px;
  .topCenter {
    width: 400px;
    height: 47px;
    line-height: 55px;
  }
  .home {
    margin-left: 10px;
  }
  .logo {
    font-size: 20px;
    color: #333;
    margin-left: 100px;
    margin-right: 55px;
    font-weight: 700;
  }
  .search {
    margin-right: 25px;
  }
}
.centerContainer {
  height: 100%;
  .wytext {
    text-align: center;
    padding-top: 1.6rem;
    img {
      width: 3.6rem;
      height: 1.3rem;
    }
  }
  .registerInput {
    margin: 1.5rem 0.5rem 0.2rem 0.5rem;
  }
  .userTest {
    margin: 0 0.5rem;
    padding: 0 0.2rem;
    height: 0.5rem;
    span {
      font-size: 0.23rem;
      margin-left: 0.2rem;
      line-height: 0.5rem;
      a {
        color: blue;
      }
    }
  }
  .register {
    margin: 1rem 0.5rem;
  }
}
</style>
